<!DOCTYPE html>



  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />







<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">





  <meta name="keywords" content="Hexo, NexT" />










<meta name="description" content="今天来谈谈前端面试中基本上每次一面的时候都会被问到的一个问题，那就是html5的新特性。这个是学习前端必须掌握的基础知识。 新增的元素html5新增了一些语义化更好的标签元素。 结构元素article元素，表示页面中的一块与上下文不相关的独立内容，比如博客中的一篇文章。aside元素，表示article内容之外的内容，辅助信息。header元素，表示页面中一个内容区块或整个页面的页眉。hgroup">
<meta name="keywords" content="君须知，未见之处，犹多宝藏">
<meta property="og:type" content="article">
<meta property="og:title" content="前端面试之html5新特性">
<meta property="og:url" content="http:&#x2F;&#x2F;yoursite.com&#x2F;2019&#x2F;11&#x2F;17&#x2F;%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E4%B9%8Bhtml5%E6%96%B0%E7%89%B9%E6%80%A7&#x2F;index.html">
<meta property="og:site_name" content="White 的个人博客">
<meta property="og:description" content="今天来谈谈前端面试中基本上每次一面的时候都会被问到的一个问题，那就是html5的新特性。这个是学习前端必须掌握的基础知识。 新增的元素html5新增了一些语义化更好的标签元素。 结构元素article元素，表示页面中的一块与上下文不相关的独立内容，比如博客中的一篇文章。aside元素，表示article内容之外的内容，辅助信息。header元素，表示页面中一个内容区块或整个页面的页眉。hgroup">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2019-11-17T09:04:31.565Z">
<meta name="twitter:card" content="summary">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="http://yoursite.com/2019/11/17/前端面试之html5新特性/"/>





  <title>前端面试之html5新特性 | White 的个人博客</title>
  








</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>
<a href="https://github.com/whitechiina" target="_blank" rel="noopener" class="github-corner" aria-label="View source on GitHub"><svg width="80" height="80" viewBox="0 0 250 250" style="fill:#f55555; color:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">White 的个人博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <p class="site-subtitle">君须知，未见之处，犹多宝藏</p>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2019/11/17/%E5%89%8D%E7%AB%AF%E9%9D%A2%E8%AF%95%E4%B9%8Bhtml5%E6%96%B0%E7%89%B9%E6%80%A7/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="White">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/images/1.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="White 的个人博客">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">前端面试之html5新特性</h1>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2019-11-17T16:46:57+08:00">
                2019-11-17
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/HTML/" itemprop="url" rel="index">
                    <span itemprop="name">HTML</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
          

          
          

          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  4k字
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  14分钟
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>今天来谈谈前端面试中基本上每次一面的时候都会被问到的一个问题，那就是html5的新特性。这个是学习前端必须掌握的基础知识。</p>
<h2 id="新增的元素"><a href="#新增的元素" class="headerlink" title="新增的元素"></a>新增的元素</h2><p>html5新增了一些语义化更好的标签元素。</p>
<h4 id="结构元素"><a href="#结构元素" class="headerlink" title="结构元素"></a>结构元素</h4><p>article元素，表示页面中的一块与上下文不相关的独立内容，比如博客中的一篇文章。<br>aside元素，表示article内容之外的内容，辅助信息。<br>header元素，表示页面中一个内容区块或整个页面的页眉。<br>hgroup元素，用于对页面中一个区块或整个页面的标题进行组合。<br>footer元素，表示页面中一个内容区块或整个页面的页脚。<br>figure元素，表示媒介内容的分组，以及它们的标题。<br>section元素，表示页面中一个内容区块，比如章节。<br>nav元素，表示页面中的导航链接。</p>
<h4 id="其他元素"><a href="#其他元素" class="headerlink" title="其他元素"></a>其他元素</h4><p>video元素，用来定义视频。<br>audio元素，用来定义音频。<br>canvas元素，用来展示图形，该元素本身没有行为，仅提供一块画布。<br>embed元素，用来插入各种多媒体，格式可以是Midi、Wav、AIFF、AU、MP3等。<br>mark元素，用来展示高亮的文字。<br>progress元素，用来展示任何类型的任务的进度。<br>meter元素，表示度量衡，定义预定义范围内的度量。<br>time元素，用来展示日期或者时间。<br>command元素，表示命令按钮。<br>details元素，用来展示用户要求得到并且可以得到的细节信息。<br>summary元素，用来为details元素定义可见的标题。<br>datalist元素，用来展示可选的数据列表，与input元素配合使用，可以制作出输入值的下拉列表。<br>datagrid元素，也用来展示可选的数据列表，以树形列表的形式来显示。<br>keygen元素，表示生成密匙。<br>output元素，表示不同类型的输出。<br>source元素，为媒介元素定义媒介资源。<br>m enu 元素，表示菜单列表。<br>ruby元素，表示ruby注释， rt元素表示字符的解释或发音。 rp元素在ruby注释中使用，以定义不支持ruby元素的浏览器所显示的内容。<br>wbr元素，表示软换行。与br元素的区别是：br元素表示此处必须换行，而wbr元素的意思是浏览器窗口或父级元素的宽度够宽时。不进行换行，而当宽度不够时，主动在此处进行换行。<br>bdi元素，定义文本的文本方向，使其脱离其周围文本的方向设置。<br>dialog元素，表示对话框或窗口。</p>
<h2 id="废除的元素"><a href="#废除的元素" class="headerlink" title="废除的元素"></a>废除的元素</h2><p>html5中废除了一些纯表现的元素，只有部分浏览器支持的元素还有一些会对可用性产生负面影响的元素。</p>
<h4 id="纯表现元素"><a href="#纯表现元素" class="headerlink" title="纯表现元素"></a>纯表现元素</h4><p>纯表现的元素就是那些可以用css替代的元素。basefont、big、center、font、s、strike、tt、u这些元素，他们的功能都是纯粹为页面展示服务的，html5提倡把页面展示性功能放在css样式表中统一处理，所以将这些元素废除，用css样式进行替代。</p>
<h4 id="对可用性产生负面影响的元素"><a href="#对可用性产生负面影响的元素" class="headerlink" title="对可用性产生负面影响的元素"></a>对可用性产生负面影响的元素</h4><p>对于frameset元素、frame元素与noframes元素，由于frame框架对网页可用性存在负面影响，在html5中已不支持frame框架，只支持iframe框架，html5中同时将frameset、frame和noframes这三个元素废除。</p>
<h4 id="只有部分浏览器支持的元素"><a href="#只有部分浏览器支持的元素" class="headerlink" title="只有部分浏览器支持的元素"></a>只有部分浏览器支持的元素</h4><p>对于applet、bgsound、blink、marquee等元素，由于只有部分浏览器支持，特别是bgsound元素以及marquee元素，只被IE支持，所以在html5中被废除。其中applet元素可由embed元素或object元素替代，bgsound元素可由audio元素替代，marquee可以由javascript编程的方式替代。</p>
<h2 id="新增的API"><a href="#新增的API" class="headerlink" title="新增的API"></a>新增的API</h2><h4 id="Canvas-API"><a href="#Canvas-API" class="headerlink" title="Canvas API"></a>Canvas API</h4><p>上文提到的canvas元素可以为页面提供一块画布来展示图形。结合Canvas API，就可以在这块画布上动态生成和展示各种图形、图表、图像以及动画了。Canvas本质上是位图画布，不可缩放，绘制出来的对象不属于页面DOM结构或者任何命名空间。不需要将每个图元当做对象存储，执行性能非常好。</p>
<p>利用Canvas API进行绘图，首先要获取canvas元素的上下文，然后用该上下文中封装的各种绘图功能进行绘图。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">"canvas"</span>&gt;</span>替代内容<span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="javascript">    <span class="keyword">var</span> canvas = <span class="built_in">document</span>.getElementById(<span class="string">'canvas'</span>);</span></span><br><span class="line"><span class="actionscript">    <span class="keyword">var</span> context =canvas.getContext(<span class="string">"2d"</span>); <span class="comment">// 获取上下文</span></span></span><br><span class="line"><span class="actionscript">    <span class="comment">//设置纯色</span></span></span><br><span class="line"><span class="actionscript">    context.fillStyle = <span class="string">"red"</span>;</span></span><br><span class="line"><span class="actionscript">    context.strokeStyle = <span class="string">"blue"</span>;</span></span><br><span class="line"><span class="actionscript">    <span class="comment">// 实践表明在不设置fillStyle下的默认fillStyle为black</span></span></span><br><span class="line">    context.fillRect(0, 0, 100, 100);</span><br><span class="line"><span class="actionscript">    <span class="comment">// 实践表明在不设置strokeStyle下的默认strokeStyle为black</span></span></span><br><span class="line">    context.strokeRect(120, 0, 100, 100);</span><br><span class="line"><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="SVG"><a href="#SVG" class="headerlink" title="SVG"></a>SVG</h2><p>SVG是html5的另一项图形功能，它是一种标准的矢量图形，是一种文件格式，有自己的API。html5引入了内联SVG，使得SVG元素可以直接出现在html标记中。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">svg</span> <span class="attr">height</span>=<span class="string">100</span> <span class="attr">width</span>=<span class="string">100</span>&gt;</span><span class="tag">&lt;<span class="name">circle</span> <span class="attr">cx</span>=<span class="string">50</span> <span class="attr">cy</span>=<span class="string">50</span> <span class="attr">r</span>=<span class="string">50</span> /&gt;</span><span class="tag">&lt;/<span class="name">svg</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h2 id="音频和视频"><a href="#音频和视频" class="headerlink" title="音频和视频"></a>音频和视频</h2><p>audio和video元素的出现让html5的媒体应用多了新选择，开发人员不必使用插件就能播放音频和视频。对于这两个元素，html5规范提供了通用、完整、可脚本化控制的API。<br>html5规范出来之前，在页面中播放视频的典型方式是使用Flash、QuickTime或者Windows Media插件往html中嵌入音频视频，相对这种方式，使用html5的媒体标签有两大好处。</p>
<p>作为浏览器原生支持的功能，新的audio和video元素无需安装。<br>媒体元素想Web页面提供了通用、集成和可脚本化控制的API。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">video</span> <span class="attr">src</span>=<span class="string">"video.webm"</span> <span class="attr">controls</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">object</span> <span class="attr">data</span>=<span class="string">"videoplayer.swf"</span> <span class="attr">type</span>=<span class="string">"application/x-shockwave-flash"</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">param</span> <span class="attr">name</span>=<span class="string">"movie"</span> <span class="attr">value</span>=<span class="string">"video.swf"</span> /&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">object</span>&gt;</span></span><br><span class="line">    Your browser does not support HTML5 video.</span><br><span class="line"><span class="tag">&lt;/<span class="name">video</span>&gt;</span></span><br></pre></td></tr></table></figure>

<h2 id="浏览器支持性检测"><a href="#浏览器支持性检测" class="headerlink" title="浏览器支持性检测"></a>浏览器支持性检测</h2><p>浏览器检测是否支持audio元素或者video元素最简单的方式是用脚本动态创建它，然后检测特定函数是否存在。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> hasVideo = !!(<span class="built_in">document</span>.createElement(<span class="string">'video'</span>).canPlayType);</span><br><span class="line">Geolocation API</span><br></pre></td></tr></table></figure>

<p>html5的Geolocation API（地理定位API），可以请求用户共享他们的位置。使用方法非常简单，如果用户同意，浏览器就会返回位置信息，该位置信息是通过支持html5地理定位功能的底层设备（如笔记本电脑或手机）提供给浏览器的。位置信息由纬度、经度坐标和一些其他元数据组成。</p>
<h3 id="位置信息从何而来"><a href="#位置信息从何而来" class="headerlink" title="位置信息从何而来"></a>位置信息从何而来</h3><p>Geolocation API不指定设备使用哪种底层技术来定位应用程序的用户。相反，它只是用于检索位置信息的API，而且通过该API检索到的数据只具有某种程度的准确性，并不能保证设备返回的位置是精确的。设备可以使用下列数据源：</p>
<p>IP地址<br>三维坐标<br>GPS<br>从RFID、WiFi和蓝牙到WiFi的MAC地址<br>GSM或CDMA手机的ID<br>用户自定义数据<br>使用方法:</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">// 一次更新</span></span><br><span class="line">navigator.geolocation.getCurrentPosition(updateLocation, handleLocationEror);</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">updateLocation</span>(<span class="params">position</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">var</span> latitude = position.coords.latitude;     <span class="comment">// 纬度</span></span><br><span class="line">    <span class="keyword">var</span> longitude = position.coords.longitude;   <span class="comment">// 经度</span></span><br><span class="line">    <span class="keyword">var</span> accuracy = position.coords.accuracy;     <span class="comment">// 准确度</span></span><br><span class="line">    <span class="keyword">var</span> timestamp = position.coords.timestamp;   <span class="comment">// 时间戳</span></span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 错误处理函数</span></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">handleLocationEror</span>(<span class="params">error</span>) </span>&#123;</span><br><span class="line">    ....</span><br><span class="line">&#125;</span><br><span class="line"><span class="comment">// 重复更新</span></span><br><span class="line">navigator.geolocation.watchPosition(updateLocation, handleLocationEror);</span><br><span class="line"><span class="comment">// 不再接受位置更新</span></span><br><span class="line">navigator.geolocation.clearWatch(watchId);</span><br></pre></td></tr></table></figure>


<h2 id="Communication-API"><a href="#Communication-API" class="headerlink" title="Communication API"></a>Communication API</h2><p>跨文档消息传递<br>出于安全方面的考虑，运行在同一浏览器中的框架、标签页、窗口间的通信一直都受到了严格的限制。然而，现实中存在一些合理的让不同站点的内容能在浏览器内进行交互的需求。这种情形下，如果浏览器内部能提供直接的通信机制，就能更好地组织这些应用。<br>html5中引入了一种新功能，跨文档消息通信，可以确保iframe、标签页、窗口间安全地进行跨源通信。postMessage API为发送消息的标准方式，发送消息非常简单：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.postMessage(<span class="string">'Hello, world'</span>, <span class="string">'http://www.example.com/'</span>);</span><br></pre></td></tr></table></figure>
<p>接收消息时，仅需在页面中增加一个事件处理函数。当某个消息到达时，通过检查消息的来源来决定是否对这条消息进行处理。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="built_in">window</span>.addEventListener(<span class="string">"message"</span>, messageHandler, <span class="literal">true</span>);</span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">messageHandler</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="keyword">switch</span>(e.origin) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="string">"friend.example.com"</span>:</span><br><span class="line">        <span class="comment">// 处理消息</span></span><br><span class="line">        processMessage(e.data);</span><br><span class="line">        <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>: </span><br><span class="line">        <span class="comment">// 消息来源无法识别</span></span><br><span class="line">        <span class="comment">// 消息被忽略</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>消息事件是一个拥有data(数据)和origin(源)属性的DOM事件。data属性是发送方传递的实际消息，而origin属性是发送来源。</p>
<h5 id="XMLHttpRequest-Level2"><a href="#XMLHttpRequest-Level2" class="headerlink" title="XMLHttpRequest Level2"></a>XMLHttpRequest Level2</h5><p>XMLHttpRequest API使得Ajax技术成为可能，作为XMLHttpRequest的改进版，XMLHttpRequest Level2在功能上有了很大的改进。主要两个方面：</p>
<p>跨源XMLHttpRequest<br>进度事件<br>跨源XMLHttpRequest<br>过去，XMLHttpRequest仅限于同源通信，XMLHttpRequest Level2通过CORS实现了跨源XMLHttpRequest。跨源HTTP请求包含一个Origin头部，它为服务器提供HTTP请求的源信息。</p>
<h2 id="WebSockets-API"><a href="#WebSockets-API" class="headerlink" title="WebSockets API"></a>WebSockets API</h2><p>WebSockets是html5中最强大的通信功能，它定义了一个全双工通信信道，仅通过Web上的一个Socket即可进行通信。</p>
<h4 id="WebSockets握手"><a href="#WebSockets握手" class="headerlink" title="WebSockets握手"></a>WebSockets握手</h4><p>为了建立WebSockets通信，客户端和服务器在初始握手时，将HTTP协议升级到WebSocket协议。一旦连接建立成功，就可以在全双工模式下在客户端和服务器之间来回传递WebSocket消息。</p>
<h4 id="WebSockets接口"><a href="#WebSockets接口" class="headerlink" title="WebSockets接口"></a>WebSockets接口</h4><p>除了对WebSockets协议定义外，该规范还同时定义了用于JavaScript应用程序的WebSocket接口。WebSockets接口的使用很简单。要连接远程主机，只需要新建一个WebSocket实例，提供希望连接的对端URL。</p>
<h2 id="Forms-API"><a href="#Forms-API" class="headerlink" title="Forms API"></a>Forms API</h2><h4 id="新表单元素"><a href="#新表单元素" class="headerlink" title="新表单元素"></a>新表单元素</h4><p>tel元素，表示电话号码。<br>email元素，表示电子邮件地址文本框。<br>url元素，表示网页的url。<br>search元素，用于搜索引擎，比如在站点顶部显示的搜索框。<br>range元素，特定值范围内的数值选择器，典型的显示方式是滑动条。<br>number元素，只包含数值的字段。</p>
<h4 id="未来的表单元素"><a href="#未来的表单元素" class="headerlink" title="未来的表单元素"></a>未来的表单元素</h4><p>color元素，颜色选择器，基于调色盘或者取色板进行选择。<br>datetime元素，显示完整的日期和时间，包括时区。<br>datetime-local，显示日期和时间。<br>time元素，不含时区的时间选择器和指示器。<br>date元素，日期选择器。<br>week元素，某年中的周选择器。<br>month元素，某年中的月选择器。</p>
<h4 id="新的表单特性和函数"><a href="#新的表单特性和函数" class="headerlink" title="新的表单特性和函数"></a>新的表单特性和函数</h4><p>placeholder<br>当用户还没输入值的时候，输入型控件可以通过placeholder特性向用户显示描述性说明或者提示信息。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">name</span>=<span class="string">"name"</span> <span class="attr">placeholder</span>=<span class="string">"First and last name"</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>autocomplete<br>浏览器通过autocomplete特性能够知晓是否应该保存输入值以备将来使用。</p>
<p>autofocus<br>通过autofocus特性可以指定某个表单元素获得输入焦点，每个页面上只允许出现一个autofocus特性，如果设置了多个，则相当于未指定此行为。</p>
<p>spellcheck<br>可对带有文本内容的输入控件和textarea空间控制spellcheck属性。设置完后，会询问浏览器是否应该给出拼写检查结果反馈。spellcheck属性需要赋值。</p>
<p>list特性和datalist元素<br>通过组合使用list特性和datalist元素，开发人员能够为某个输入型控件构造一张选值列表。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">datalist</span> <span class="attr">id</span>=<span class="string">"contactList"</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"a@qq.com"</span>&gt;</span><span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">option</span> <span class="attr">value</span>=<span class="string">"b@qq.com"</span>&gt;</span><span class="tag">&lt;/<span class="name">option</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">datalist</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">input</span> <span class="attr">type</span>=<span class="string">"email"</span> <span class="attr">id</span>=<span class="string">"contatcs"</span> <span class="attr">list</span>=<span class="string">"contactList"</span>&gt;</span></span><br></pre></td></tr></table></figure>


<p>min和max<br>通过设置min和max特性，可以将range输入框的数值输入范围限定在最低值和最高值之间。可以只设置一个，也可以两个都设置，也可以都不设置。</p>
<p>step<br>对于输入型控件，设置其step特性能够指定输入值递增或者递减的粒度。</p>
<p>required<br>一旦为某输入型控件设置了required特性，那么此项必填，否则无法提交表单。</p>
<h2 id="拖放API"><a href="#拖放API" class="headerlink" title="拖放API"></a>拖放API</h2><p>draggable属性<br>如果网页元素的draggable元素为true，这个元素就是可以拖动的。</p>
<figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">div</span> <span class="attr">draggable</span>=<span class="string">"true"</span>&gt;</span>Draggable Div<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span><br></pre></td></tr></table></figure>


<p>拖放事件<br>拖动过程会触发很多事件，主要有下面这些：</p>
<p>dragstart：网页元素开始拖动时触发。<br>drag：被拖动的元素在拖动过程中持续触发。<br>dragenter：被拖动的元素进入目标元素时触发，应在目标元素监听该事件。<br>dragleave：被拖动的元素离开目标元素时触发，应在目标元素监听该事件。<br>dragover：被拖动元素停留在目标元素之中时持续触发，应在目标元素监听该事件。<br>drap：被拖动元素或从文件系统选中的文件，拖放落下时触发。<br>dragend：网页元素拖动结束时触发。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">draggableElement.addEventListener(<span class="string">'dragstart'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">e</span>) </span>&#123;</span><br><span class="line">    <span class="built_in">console</span>.log(<span class="string">'拖动开始！'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p>dataTransfer对象<br>拖动过程中，回调函数接受的事件参数，有一个dataTransfer属性，指向一个对象，包含与拖动相关的各种信息。</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">draggableElement.addEventListener(<span class="string">'dragstart'</span>, <span class="function"><span class="keyword">function</span>(<span class="params">event</span>) </span>&#123;</span><br><span class="line">    event.dataTransfer.setData(<span class="string">'text'</span>, <span class="string">'Hello World!'</span>);</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>dataTransfer对象的属性有：</p>
<p>dropEffect：拖放的操作类型，决定了浏览器如何显示鼠标形状，可能的值为copy、move、link和none。<br>effectAllowed：指定所允许的操作，可能的值为copy、move、link、copyLink、copyMove、linkMove、all、none和uninitialized（默认值，等同于all，即允许一切操作）。<br>files：包含一个FileList对象，表示拖放所涉及的文件，主要用于处理从文件系统拖入浏览器的文件。<br>types：储存在DataTransfer对象的数据的类型。<br>dataTransfer对象的方法有：</p>
<p>setData(format, data)：在dataTransfer对象上储存数据。第一个参数format用来指定储存的数据类型，比如text、url、text/html等。<br>getData(format)：从dataTransfer对象取出数据。<br>clearData(format)：清除dataTransfer对象所储存的数据。如果指定了format参数，则只清除该格式的数据，否则清除所有数据。<br>setDragImage(imgElement, x, y)：指定拖动过程中显示的图像。默认情况下，许多浏览器显示一个被拖动元素的半透明版本。参数imgElement必须是一个图像元素，而不是指向图像的路径，参数x和y表示图像相对于鼠标的位置。</p>
<h3 id="Web-Workers-API"><a href="#Web-Workers-API" class="headerlink" title="Web Workers API"></a>Web Workers API</h3><p>Javascript是单线程的。因此，持续时间较长的计算，回阻塞UI线程，进而导致无法在文本框中填入文本，单击按钮等，并且在大多数浏览器中，除非控制权返回，否则无法打开新的标签页。该问题的解决方案是Web Workers，可以让Web应用程序具备后台处理能力，对多线程的支持性非常好。</p>
<p>但是在Web Workers中执行的脚本不能访问该页面的window对象，也就是Web Workers不能直接访问Web页面和DOM API。虽然Web Workers不会导致浏览器UI停止响应，但是仍然会消耗CPU周期，导致系统反应速度变慢。</p>
<h3 id="Web-Storage-API"><a href="#Web-Storage-API" class="headerlink" title="Web Storage API"></a>Web Storage API</h3><p>Web Storage是html5引入的一个非常重要的功能，可以在客户端本地存储数据，类似html4的cookie，但可实现功能要比cookie强大的多。</p>
<h5 id="sessionStorage"><a href="#sessionStorage" class="headerlink" title="sessionStorage"></a>sessionStorage</h5><p>sessionStorage将数据保存在session中，浏览器关闭数据就消失。</p>
<h5 id="localStorage"><a href="#localStorage" class="headerlink" title="localStorage"></a>localStorage</h5><p>localStorage则一直将数据保存在客户端本地，除非手动删除，否则一直保存。<br>不管是sessionStorage，还是localStorage，可使用的API相同，常用的有如下几个（以localStorage为例）：</p>
<p>保存数据：localStorage.setItem(key,value);<br>读取数据：localStorage.getItem(key);<br>删除单个数据：localStorage.removeItem(key);<br>删除所有数据：localStorage.clear();<br>得到某个索引的key：localStorage.key(index);</p>

      
    </div>
    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2019/11/17/CSS%E8%87%AA%E9%80%82%E5%BA%94%E5%B8%83%E5%B1%80%E6%80%BB%E7%BB%93%E6%95%99%E7%A8%8B/" rel="next" title="CSS自适应布局总结教程">
                <i class="fa fa-chevron-left"></i> CSS自适应布局总结教程
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2019/11/18/Markdown%E7%9A%84%E5%B8%B8%E7%94%A8%E8%AF%AD%E6%B3%95/" rel="prev" title="Markdown的常用语法">
                Markdown的常用语法 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/images/1.jpg"
                alt="White" />
            
              <p class="site-author-name" itemprop="name">White</p>
              <p class="site-description motion-element" itemprop="description">君须知，未见之处，犹多宝藏</p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%7C%7Carchive">
              
                  <span class="site-state-item-count">10</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">7</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/yourname" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#新增的元素"><span class="nav-number">1.</span> <span class="nav-text">新增的元素</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#结构元素"><span class="nav-number">1.0.1.</span> <span class="nav-text">结构元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#其他元素"><span class="nav-number">1.0.2.</span> <span class="nav-text">其他元素</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#废除的元素"><span class="nav-number">2.</span> <span class="nav-text">废除的元素</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#纯表现元素"><span class="nav-number">2.0.1.</span> <span class="nav-text">纯表现元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#对可用性产生负面影响的元素"><span class="nav-number">2.0.2.</span> <span class="nav-text">对可用性产生负面影响的元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#只有部分浏览器支持的元素"><span class="nav-number">2.0.3.</span> <span class="nav-text">只有部分浏览器支持的元素</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#新增的API"><span class="nav-number">3.</span> <span class="nav-text">新增的API</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#Canvas-API"><span class="nav-number">3.0.1.</span> <span class="nav-text">Canvas API</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#SVG"><span class="nav-number">4.</span> <span class="nav-text">SVG</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#音频和视频"><span class="nav-number">5.</span> <span class="nav-text">音频和视频</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#浏览器支持性检测"><span class="nav-number">6.</span> <span class="nav-text">浏览器支持性检测</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#位置信息从何而来"><span class="nav-number">6.1.</span> <span class="nav-text">位置信息从何而来</span></a></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Communication-API"><span class="nav-number">7.</span> <span class="nav-text">Communication API</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#XMLHttpRequest-Level2"><span class="nav-number">7.0.0.1.</span> <span class="nav-text">XMLHttpRequest Level2</span></a></li></ol></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#WebSockets-API"><span class="nav-number">8.</span> <span class="nav-text">WebSockets API</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#WebSockets握手"><span class="nav-number">8.0.1.</span> <span class="nav-text">WebSockets握手</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#WebSockets接口"><span class="nav-number">8.0.2.</span> <span class="nav-text">WebSockets接口</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#Forms-API"><span class="nav-number">9.</span> <span class="nav-text">Forms API</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#新表单元素"><span class="nav-number">9.0.1.</span> <span class="nav-text">新表单元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#未来的表单元素"><span class="nav-number">9.0.2.</span> <span class="nav-text">未来的表单元素</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#新的表单特性和函数"><span class="nav-number">9.0.3.</span> <span class="nav-text">新的表单特性和函数</span></a></li></ol></li></ol></li><li class="nav-item nav-level-2"><a class="nav-link" href="#拖放API"><span class="nav-number">10.</span> <span class="nav-text">拖放API</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#Web-Workers-API"><span class="nav-number">10.1.</span> <span class="nav-text">Web Workers API</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#Web-Storage-API"><span class="nav-number">10.2.</span> <span class="nav-text">Web Storage API</span></a><ol class="nav-child"><li class="nav-item nav-level-5"><a class="nav-link" href="#sessionStorage"><span class="nav-number">10.2.0.1.</span> <span class="nav-text">sessionStorage</span></a></li><li class="nav-item nav-level-5"><a class="nav-link" href="#localStorage"><span class="nav-number">10.2.0.2.</span> <span class="nav-text">localStorage</span></a></li></ol></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2019</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">White</span>

  
    <span class="post-meta-divider">|</span>
    <span class="post-meta-item-icon">
      <i class="fa fa-heart"></i>
    </span>
    
      <span class="post-meta-item-text">Site words total count&#58;</span>
    
    <span title="Site words total count">26.9k</span>
  
</div>



<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">博客全站共26.9k 字</span>
</div>

        







        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
          <span id="scrollpercent"><span>0</span>%</span>
        
      </div>
    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  












  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  












  





  

  

  

  
  

  

  

  

  

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/hijiki.model.json"},"display":{"position":"right","hOffset":100,"width":150,"height":300},"mobile":{"show":true},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
